# -*- coding: utf-8 -*-
# @Time    : 2023/11/08 09:43
# @Author  : Jonny
# @File    : LuckDingtalkUserService.py
# @Describe: 钉钉逻辑服务层

import logging
import base64
import requests
import json

class LuckDingtalkUserService(object):

    def __init__(self, config):
        self.config = config

    CONTENT_TYPE = 'application/json'
    # 读取指定openid的用户信息
    def read_user_by_id(self, dingtalk_userid):
        try:
            dingtalk_app_key = self.config["dingtalk_app_key"]
            dingtalk_app_secret = self.config["dingtalk_app_secret"]
            dingtalk_url = self.config["dingtalk_url"]
            dingtalk_url_basic_auth1 = self.config["dingtalk_url_basic_auth1"]
            dingtalk_url_basic_auth2 = self.config["dingtalk_url_basic_auth2"]

            auth = str(base64.b64encode(f'{dingtalk_url_basic_auth1}:{dingtalk_url_basic_auth2}'.encode('utf-8')),
                       'utf-8')
            headers = {
                'Content-Type': self.CONTENT_TYPE,
                'Authorization': f'Basic {auth}'
            }
            url = f"{dingtalk_url}/user/readAll"

            if dingtalk_userid is None or dingtalk_userid == "":
                dingtalk_userid = "all"
            msg = {}
            if "all" == dingtalk_userid:
                return None
            else:
                msg = {
                    "dingtalk_app_key": dingtalk_app_key,
                    "dingtalk_app_secret": dingtalk_app_secret,
                    "user_id": dingtalk_userid  # 钉钉openid
                }

            res = requests.post(url, data=json.dumps(msg), headers=headers)
            result = res.json()
            if res.status_code == 200:
                data = result["data"]
                if data is not None and len(data) > 0:
                    return data[0]
            return None
        except Exception as e:
            raise e

    # 读取指定工号的用户信息
    def read_user_by_gh(self, dingtalk_usergh):
        try:
            dingtalk_app_key = self.config["dingtalk_app_key"]
            dingtalk_app_secret = self.config["dingtalk_app_secret"]
            dingtalk_url = self.config["dingtalk_url"]
            dingtalk_url_basic_auth1 = self.config["dingtalk_url_basic_auth1"]
            dingtalk_url_basic_auth2 = self.config["dingtalk_url_basic_auth2"]

            auth = str(base64.b64encode(f'{dingtalk_url_basic_auth1}:{dingtalk_url_basic_auth2}'.encode('utf-8')),
                       'utf-8')
            headers = {
                'Content-Type': self.CONTENT_TYPE,
                'Authorization': f'Basic {auth}'
            }
            url = f"{dingtalk_url}/user/readAll"

            if dingtalk_usergh is None or dingtalk_usergh == "":
                dingtalk_usergh = "all"
            msg = {}
            if "all" == dingtalk_usergh:
                return None
            else:
                msg = {
                    "dingtalk_app_key": dingtalk_app_key,
                    "dingtalk_app_secret": dingtalk_app_secret,
                    "user_job_number": dingtalk_usergh  # 钉钉工号
                }

            res = requests.post(url, data=json.dumps(msg), headers=headers)
            result = res.json()
            if res.status_code == 200:
                data = result["data"]
                if data is not None and len(data) > 0:
                    return data[0]
            return None
        except Exception as e:
            raise e
